<template>
  <div>
    <div style="margin-bottom: 20px">
      <el-button round @click="openDialog">创建角色</el-button>
      <el-button round :disabled="currentRow == null" @click="handleUpdateAuth"
        >设置角色权限</el-button
      >
    </div>
    <el-table
      :data="roleList"
      style="width: 100%"
      class="role_table"
      ref="singleTable"
      highlight-current-row
      @current-change="handleCurrentChange"
      border
      height="313"
    >
      <el-table-column type="index" label="序号" width="100" align="center">
      </el-table-column>
      <el-table-column
        label="创建日期"
        width="200"
        align="center"
        property="create_time"
      ></el-table-column>
      <el-table-column
        label="角色名称"
        width="200"
        align="center"
        property="name"
      ></el-table-column>
      <el-table-column
        label="授权时间"
        width="200"
        align="center"
        property="auth_time"
      ></el-table-column>
      <el-table-column
        label="授权人"
        width="200"
        align="center"
        property="auth_name"
      ></el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentPage"
      :current-page="currentPage"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <!-- 新增窗口 -->
    <el-dialog title="创建角色" :visible.sync="dialogFormVisible" width="400px">
      <el-form
        :model="role"
        status-icon
        ref="roleForm"
        label-width="100px"
        label-position="left"
        style="width: 90%"
        :rules="roleRules"
      >
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="role.name" placehilder="请输入角色名称"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addData('roleForm')">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 为角色设置权限 -->
    <el-dialog
      title="设置角色权限"
      :visible.sync="roleAuthVisible"
      width="400px"
    >
      <Auth :role="currentRow" ref="auth"></Auth>
      <div slot="footer" class="dialog-footer">
        <el-button @click="roleAuthVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateRole">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import roleApi from "@/api/roles";
import memoryUtils from "@/utils/memoryUtils";
import Auth from "./Auth.vue";
import {formateDate} from "@/utils/dateUtils"
export default {
  data() {
    return {
      total: 0,
      size: 5, //当前页个数
      currentPage: 1, //当前页
      roleAuthVisible: false,
      dialogFormVisible: false,
      role: {
        name: "",
        menus: [],
      },
      roleRules: {
        name: [
          { required: true, message: "角色名称必须输入", trigger: "blur" },
        ],
      },
      currentRow: null,
      roleList: [], //存储角色列表
    };
  },

  components: { Auth },
  mounted() {
    this.condition();
  },
  methods: {
    // 获取分页信息
    condition() {
      roleApi.condition(this.currentPage, this.size).then((response) => {
        const res = response.data;
        if (res.status == 0) {
          this.total = res.data.total;
          console.log(res.data.data);
          res.data.data.map((item) => {
            item.create_time = formateDate(item.create_time);
            item.auth_time = formateDate(item.auth_time);
          });
          this.roleList = res.data.data;
          if (this.roleList == 0) {
            this.currentPage = this.currentPage - 1;
            this.condition();
          }
        } else {
          this.$message({
            message: res.msg,
            type: "warning",
          });
        }
      });
    },
    // 改变当前页位置
    handleCurrentPage(currentPage) {
      this.currentPage = currentPage;
      // console.log(currentPage);
      this.condition();
    },
    // 每页条数
    handleSizeChange(size) {
      // console.log(size);
      this.size = size;
      this.condition();
    },
    // 自带函数获取当前点击行，获取两个参数，一个当前点击的一个为上一次点击的行
    handleCurrentChange(val) {
      this.currentRow = val;
      // console.log(val);
    },
    // 删除
    handleDelete(index, row) {
      // console.log(index, row);
      roleApi.deleteRole(row).then((response) => {
        const res = response.data;
        // console.log(res);
        if (res.status == 0) {
          this.$message({
            message: res.data,
            type: "success",
          });
          this.condition();
        } else {
          this.$message({
            message: res.msg,
            type: "warning",
          });
        }
      });
    },
    // 获取角色信息
    fetchData() {
      roleApi.getRolesList().then((resole) => {
        const res = resole.data;
        if (res.status == 0) {
          this.roleList = res.data;
        } else {
          this.$message({
            message: res.msg,
            type: "warning",
          });
        }
      });
    },
    // 创建角色
    openDialog() {
      this.dialogFormVisible = true;
      // 异步事件
      this.$nextTick(() => {
        this.$refs["roleForm"].resetFields();
      });
    },
    // 提交新增数据
    addData(fromName) {
      this.$refs[fromName].validate((valid) => {
        if (valid) {
          roleApi.addRole(this.role.name).then((response) => {
            const res = response.data;
            if (res.status == 0) {
              this.$message({
                message: "添加成功",
                type: "success",
              });
            } else {
              this.$message({
                message: "添加异常",
                type: "error",
              });
              console.log("添加异常");
              return false;
            }
          });
        } else {
          console.log("添加失败");
          return false;
        }
        this.dialogFormVisible = false;
        this.condition();
      });
    },
    // 更新权限
    updateRole() {
      const newRole = this.$refs["auth"].getMenus();
      console.log(newRole);
      this.currentRow.menus = newRole.menus;
      this.currentRow.name = newRole.name;
      console.log(memoryUtils.user);
      this.currentRow.auth_name = this.$store.state.user.username;
      console.log(this.currentRow);
      roleApi.updateRole(this.currentRow).then((response) => {
        const res = response.data;
        if (res.status == 0) {
          this.$message({
            message: "更新成功",
            type: "success",
          });
          this.roleAuthVisible = false;
          this.condition();
        } else {
          this.$message({
            message: "更新失败",
            type: "warning",
          });
        }
      });
    },
    // 打开权限窗口
    handleUpdateAuth() {
      this.roleAuthVisible = true;
    },
  },
};
</script>

<style scoped></style>